package com.dk.service.impl;

import com.dk.data.Result;
import com.dk.em.ResultEnum;
import com.dk.entity.DtContentCategory;
import com.dk.mapper.DtContentCategoryMapper;
import com.dk.service.DtContentCateGoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class DtContentCateGoryServiceImpl implements DtContentCateGoryService {
    @Resource
    private DtContentCategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<DtContentCategory> categoryList() {
        String key = "category";
        if (redisTemplate.hasKey(key)) {
          return   (List<DtContentCategory>)redisTemplate.opsForValue().get(key);
        }else {
            List<DtContentCategory> categoryList1 = categoryMapper.selectList(null);
            redisTemplate.opsForValue().set(key,categoryList1);
            redisTemplate.expire(key,30, TimeUnit.SECONDS);
            return categoryList1;
        }
    }
}
